var gulp = require('gulp');
var connect = require('gulp-connect');
var babel = require('gulp-babel');
var sourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var less = require('gulp-less');
var cleanCss = require('gulp-clean-css');
var autoprefixer = require('autoprefixer');
var postCss = require('gulp-postcss');
var proxy = require('http-proxy-middleware');
var clean   = require('gulp-clean');
var pump    = require('pump');
var del = require('del');


// 起服务
gulp.task('connect', function() {
    connect.server({
        root: 'dist',
        livereload: true,
        port: 3000,
        //如果没有host, 就自己添加一个
        host:'0.0.0.0'
    // middleware: function(connect, opt) {
    //        return [
    //             proxy('/printbox',  {
    //                 target: 'http://172.20.8.30:8891',
    //                 changeOrigin:true
    //            })
    //        ]
    //    }
    })
})

gulp.task('clean', function (cb) {
    del([
        'dist/*'
    ], cb);
});

// html文件
gulp.task('html', function() {
    gulp.src('./src/**/**/*.html')
        .pipe(connect.reload())
        .pipe(gulp.dest('./dist/'));
})

// css文件
gulp.task('less', ['html'], function() {
    gulp.src('./src/**/**/*.less')
        .pipe(less())
        .pipe(postCss([autoprefixer({browsers: ['last 2 versions']})]))
        .pipe(connect.reload())
        .pipe(gulp.dest('./dist/'))
})

// js文件
gulp.task('js',['html'] , function () {
    gulp.src('./src/**/**/*.js')
        .pipe(connect.reload())
        .pipe(gulp.dest('./dist/'))
})

// 图片
gulp.task('img', function () {
    gulp.src('./src/**/**/*.*')
    .pipe(connect.reload())
        .pipe(gulp.dest('./dist/'));
})

// 监控
gulp.task('watch', function() {
    gulp.watch(['./src/**/**/*.html'], ['html']);

    gulp.watch(['./src/js/**/*.js'], ['js']);

    gulp.watch(['./src/css/**/*.less'], ['less']);
})

// 默认任务
gulp.task('default', ['clean','img','connect', 'watch', 'js', 'less']);